package com.eptok.gw.szcups.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import com.eptok.gw.szcups.util.SerialNumber;


/**
 *@author:heqishan, Email:heqishan@gmail.com
 *2013-3-1 下午03:03:30
 */
public class PropertyLoader {
	
	private String localIp;
	private String hsmIp;
	private int hsmPort;
	private File settingFile;
	
	private int chanServerPort;
	private String chanClientIp;
	private int chanClientPort;
	private String chanAcquirerPosID;
	private String chanForwardPosID;
	private String chanTerminalID;
	private String chanParterID;
	private String chanParterName;
	
	
	private String activeMerParterType;
	private int activeMerServerPort;
	private String activeMerClientIp;
	private int activeMerClientPort;
	private String activeMerAcquirerPosID;
	private String activeMerForwardPosID;
	private String activeMerTerminalID;
	private String activeMerParterID;
	private String activeMerParterName;
	
	
	private String passiveMerClientIp;
	private int passiveMerClientPort;
	private int passiveMerServerPort;
	
	
	
	//固定配置文件
	private static final String FIX_FILE_NAME = "config.properties";
	
	//可变动配置文件
	private static final String CHANGE_FILE_NAME = "setting.properties";
	
	private static PropertyLoader propertyLoader = new PropertyLoader();
	
	private PropertyLoader(){}
	
	public static PropertyLoader getInstance(){
		return propertyLoader;
	}
	
	public void loadProperties(String configPath) throws Exception {
		//实例化configFile
		 File fixConfigFile = new File(configPath +  File.separator + "conf" + 
				File.separator + FIX_FILE_NAME);
		 
		 settingFile = new File(configPath +  File.separator + "conf" + 
					File.separator + CHANGE_FILE_NAME);
		
		//读取固定配置文件
		final Properties p = new Properties();
		p.load(new FileInputStream(fixConfigFile));
		
		localIp = p.getProperty("localIp").trim();
		hsmIp = p.getProperty("hsmIp").trim();
		hsmPort = Integer.parseInt(p.getProperty("hsmPort").trim());
		
		chanServerPort = Integer.parseInt(p.getProperty("chanServerPort").trim());
		chanClientIp = p.getProperty("chanClientIp").trim();
		chanClientPort = Integer.parseInt(p.getProperty("chanClientPort").trim());
		chanAcquirerPosID = p.getProperty("chanAcquirerPosID").trim();
		chanForwardPosID = p.getProperty("chanForwardPosID").trim();
		chanParterID = p.getProperty("chanParterID").trim();
		chanTerminalID = p.getProperty("chanTerminalID").trim();
		chanParterName = p.getProperty("chanParterName").trim();
		
		activeMerClientIp = p.getProperty("activeMerClientIp").trim();
		activeMerClientPort = Integer.parseInt(p.getProperty("activeMerClientPort").trim());
		activeMerServerPort = Integer.parseInt(p.getProperty("activeMerServerPort").trim());
		activeMerAcquirerPosID = p.getProperty("activeMerAcquirerPosID").trim();
		activeMerForwardPosID = p.getProperty("activeMerForwardPosID").trim();
		activeMerTerminalID = p.getProperty("activeMerTerminalID").trim();
		activeMerParterID = p.getProperty("activeMerParterID").trim();
		activeMerParterName = p.getProperty("activeMerParterName").trim();
		activeMerParterType = p.getProperty("activeMerParterType").trim();
		
		passiveMerClientIp = p.getProperty("passiveMerClientIp").trim();
		passiveMerClientPort = Integer.parseInt(p.getProperty("passiveMerClientPort").trim());
		passiveMerServerPort = Integer.parseInt(p.getProperty("passiveMerServerPort").trim());
		
		
		KeyContainer.getInstance().setSzCupsZmk(p.getProperty("szCupsZmk").trim());
		KeyContainer.getInstance().setMerCupsZmk(p.getProperty("merCupsZmk").trim());
		
		//读取可变配置文件
		final Properties p2 = new Properties();
		p2.load(new FileInputStream(settingFile));
		SerialNumber.chanTrackingNo = Integer.parseInt(p2.getProperty("chanTrackingNo").trim()) + 99;	//系统跟踪号跳着增加
		SerialNumber.activeMerTrackingNo	= Integer.parseInt(p2.getProperty("activeMerTrackingNo").trim()) + 99;	//系统跟踪号跳着增加
		
		KeyContainer.getInstance().setMerZAK(p2.getProperty("merZAK").trim());
		KeyContainer.getInstance().setMerZPK(p2.getProperty("merZPK").trim());
		
	}
	
	/**
	 * 修改配置文件
	 * @param key
	 * @param value
	 * @throws Exception
	 */
	public void storeConfig() throws Exception{
		final Properties p = new Properties();
		InputStream input = new FileInputStream(settingFile);
		OutputStream output = new FileOutputStream(settingFile);
		try{
			p.load(new FileInputStream(settingFile));
			p.setProperty("chanTrackingNo", String.valueOf(SerialNumber.chanTrackingNo));
			p.setProperty("activeMerTrackingNo", String.valueOf(SerialNumber.activeMerTrackingNo));
			p.setProperty("merZPK", KeyContainer.getInstance().getMerZPK());
			p.setProperty("merZAK", KeyContainer.getInstance().getMerZAK());
			p.store(output,  "store setting sucess!");
		}finally{
			input.close();
			output.close();
		}
	}
	
	
	public String getLocalIp() {
		return localIp;
	}

	public int getChanServerPort() {
		return chanServerPort;
	}


	public String getChanClientIp() {
		return chanClientIp;
	}

	public int getChanClientPort() {
		return chanClientPort;
	}
	
	public String getChanAcquirerPosID() {
		return chanAcquirerPosID;
	}

	public String getChanForwardPosID() {
		return chanForwardPosID;
	}

	public String getChanParterID() {
		return chanParterID;
	}

	public String getHsmIp() {
		return hsmIp;
	}

	public int getHsmPort() {
		return hsmPort;
	}

	public String getChanTerminalID() {
		return chanTerminalID;
	}

	public String getChanParterName() {
		return chanParterName;
	}

	public int getActiveMerServerPort() {
		return activeMerServerPort;
	}

	public String getActiveMerClientIp() {
		return activeMerClientIp;
	}

	public int getActiveMerClientPort() {
		return activeMerClientPort;
	}

	public String getActiveMerAcquirerPosID() {
		return activeMerAcquirerPosID;
	}

	public String getActiveMerForwardPosID() {
		return activeMerForwardPosID;
	}

	public String getActiveMerTerminalID() {
		return activeMerTerminalID;
	}

	public String getActiveMerParterID() {
		return activeMerParterID;
	}

	public String getActiveMerParterName() {
		return activeMerParterName;
	}

	public String getPassiveMerClientIp() {
		return passiveMerClientIp;
	}

	public int getPassiveMerClientPort() {
		return passiveMerClientPort;
	}

	public int getPassiveMerServerPort() {
		return passiveMerServerPort;
	}

	public static PropertyLoader getPropertyLoader() {
		return propertyLoader;
	}

	public String getActiveMerParterType() {
		return activeMerParterType;
	}

}
